package com.hnit.applet.utils;

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @ClassName: RsaKeyHelper
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author XiongXiaoQing
 * @date 2018年10月31日
 *
 */
public class RsaKeyHelper {

	/**
	 * 
	 * @Title: getPublicKey @Description: 获取公钥 @param @param
	 * filename @param @return @param @throws Exception 参数 @return PublicKey
	 * 返回类型 @throws
	 */
	public PublicKey getPublicKey(String filename) throws Exception {
		InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(filename);
		DataInputStream dis = new DataInputStream(resourceAsStream);
		byte[] keyBytes = new byte[resourceAsStream.available()];
		dis.readFully(keyBytes);
		dis.close();
		X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
		KeyFactory kf = KeyFactory.getInstance("RSA");
		return kf.generatePublic(spec);
	}

	/**
	 * 
	 * @Title: getPrivateKey @Description: 获取密钥 @param @param
	 * filename @param @return @param @throws Exception 参数 @return PrivateKey
	 * 返回类型 @throws
	 */
	public PrivateKey getPrivateKey(String filename) throws Exception {
		InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(filename);
		DataInputStream dis = new DataInputStream(resourceAsStream);
		byte[] keyBytes = new byte[resourceAsStream.available()];
		dis.readFully(keyBytes);
		dis.close();
		PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory kf = KeyFactory.getInstance("RSA");
		return kf.generatePrivate(spec);
	}

	/**
	 * 
	 * @Title: getPublicKey @Description: 获取公钥 @param @param
	 * publicKey @param @return @param @throws Exception 参数 @return PublicKey
	 * 返回类型 @throws
	 */
	public PublicKey getPublicKey(byte[] publicKey) throws Exception {
		X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKey);
		KeyFactory kf = KeyFactory.getInstance("RSA");
		return kf.generatePublic(spec);
	}

	/**
	 * 
	 * @Title: getPrivateKey @Description: 获取密钥 @param @param
	 * privateKey @param @return @param @throws Exception 参数 @return PrivateKey
	 * 返回类型 @throws
	 */
	public PrivateKey getPrivateKey(byte[] privateKey) throws Exception {
		PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKey);
		KeyFactory kf = KeyFactory.getInstance("RSA");
		return kf.generatePrivate(spec);
	}

	/**
	 * 
	 * @Title: generateKey @Description: 生存rsa公钥和密钥 @param @param
	 * publicKeyFilename @param @param privateKeyFilename @param @param
	 * password @param @throws IOException @param @throws
	 * NoSuchAlgorithmException 参数 @return void 返回类型 @throws
	 */
	public void generateKey(String publicKeyFilename, String privateKeyFilename, String password)
			throws IOException, NoSuchAlgorithmException {
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
		SecureRandom secureRandom = new SecureRandom(password.getBytes());
		keyPairGenerator.initialize(2024, secureRandom);
		KeyPair keyPair = keyPairGenerator.genKeyPair();
		byte[] publicKeyBytes = keyPair.getPublic().getEncoded();
		FileOutputStream fos = new FileOutputStream(publicKeyFilename);
		fos.write(publicKeyBytes);
		fos.close();
		byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();
		fos = new FileOutputStream(privateKeyFilename);
		fos.write(privateKeyBytes);
		fos.close();
	}

	/**
	 * 
	 * @Title: generatePublicKey @Description: 生存rsa公钥 @param @param
	 * password @param @return @param @throws IOException @param @throws
	 * NoSuchAlgorithmException 参数 @return byte[] 返回类型 @throws
	 */
	public static byte[] generatePublicKey(String password) throws IOException, NoSuchAlgorithmException {
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
		SecureRandom secureRandom = new SecureRandom(password.getBytes());
		keyPairGenerator.initialize(1024, secureRandom);
		KeyPair keyPair = keyPairGenerator.genKeyPair();
		return keyPair.getPublic().getEncoded();
	}

	/**
	 * 
	 * @Title: generatePrivateKey @Description: 生存rsa公钥 @param @param
	 * password @param @return @param @throws IOException @param @throws
	 * NoSuchAlgorithmException 参数 @return byte[] 返回类型 @throws
	 */
	public static byte[] generatePrivateKey(String password) throws IOException, NoSuchAlgorithmException {
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
		SecureRandom secureRandom = new SecureRandom(password.getBytes());
		keyPairGenerator.initialize(1024, secureRandom);
		KeyPair keyPair = keyPairGenerator.genKeyPair();
		return keyPair.getPrivate().getEncoded();
	}

	public static Map<String, byte[]> generateKey(String password) throws IOException, NoSuchAlgorithmException {
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
		SecureRandom secureRandom = new SecureRandom(password.getBytes());
		keyPairGenerator.initialize(1024, secureRandom);
		KeyPair keyPair = keyPairGenerator.genKeyPair();
		byte[] publicKeyBytes = keyPair.getPublic().getEncoded();
		byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();
		Map<String, byte[]> map = new HashMap<String, byte[]>();
		map.put("pub", publicKeyBytes);
		map.put("pri", privateKeyBytes);
		return map;
	}

}
